namespace Peer.Web.Controls.Upload
{
    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class WebbUpload
    {
        private string m_GUID;
        private string m_script = string.Empty;

        public WebbUpload()
        {
            if (WebbHelper.IsAccordantBrowser())
            {
                this.m_script = "\r\n\t\t\t\t\t<script language=javascript>\r\n\t\t\t\t\t<!--\r\n\t\t\t\t\turl='${url}$';\r\n\t\t\t\t\tvar submited = false;\r\n\t\t\t\t\tfunction openProgress()\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif(!submited)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tvar ary = document.getElementsByTagName('INPUT');\r\n\t\t\t\t\t\t\tvar openBar = false;\r\n\t\t\t\t\t\t\tfor(var i=0;i<ary.length;i++)\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tvar obj = ary[i];\r\n\t\t\t\t\t\t\t\tif(obj.type  == 'file')\r\n\t\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\t\tif(obj.value != '')\r\n\t\t\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\t\t\topenBar = true;\r\n\t\t\t\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\tif(openBar)\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\twindow.showModelessDialog(url, window, 'status:no;help:no;resizable:no;scroll:no;dialogWidth:398px;dialogHeight:200px');\r\n\t\t\t\t\t\t\t\tsubmited = true;\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\treturn true;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tevent.srcElement.disabled = true;\r\n\t\t\t\t\t\t\treturn false;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\t//-->\r\n\t\t\t\t\t</script>";
            }
            else
            {
                this.m_script = "\r\n\t\t\t\t\t<script language=javascript>\r\n\t\t\t\t\t<!--\r\n\t\t\t\t\turl='${url}$';\r\n\t\t\t\t\tvar submited = false;\r\n\t\t\t\t\tfunction openProgress()\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif(!submited)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tvar ary = document.getElementsByTagName('INPUT');\r\n\t\t\t\t\t\t\tvar openBar = false;\r\n\t\t\t\t\t\t\tfor(var i=0;i<ary.length;i++)\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tvar obj = ary[i];\r\n\t\t\t\t\t\t\t\tif(obj.type  == 'file')\r\n\t\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\t\tif(obj.value != '')\r\n\t\t\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\t\t\topenBar = true;\r\n\t\t\t\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\tif(openBar)\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tvar swd = window.screen.availWidth;\r\n\t\t\t\t\t\t\t\tvar sht = window.screen.availHeight;\r\n\t\t\t\t\t\t\t\tvar wd = 398;\r\n\t\t\t\t\t\t\t\tvar ht =170;\r\n\t\t\t\t\t\t\t\tvar left = (swd-wd)/2;\r\n\t\t\t\t\t\t\t\tvar top = (sht-ht)/2;\r\n\t\t\t\t\t\t\t\twindow.open(url,'_blank','status=no,toolbar=no,menubar=no,location=no,height='+ht+',width='+wd+',left='+left+',top='+top, true);\r\n\t\t\t\t\t\t\t\tsubmited = true;\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\treturn true;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tevent.srcElement.disabled = true;\r\n\t\t\t\t\t\t\treturn false;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\t//-->\r\n\t\t\t\t\t</script>";
            }
            this.m_GUID = string.Empty;
        }

        public UploadFile GetUploadFile(string name)
        {
            UploadFile file = new UploadFile(name);
            if (!(file.FileName == string.Empty))
            {
                return file;
            }
            return null;
        }

        public UploadFileCollection GetUploadFileList(string name)
        {
            UploadFileCollection files = new UploadFileCollection();
            string str = WebbHelper.GetContext().Request[name];
            if(string.IsNullOrEmpty(str))
                return files;
            foreach (string str2 in str.Split(new char[] { ',' }))
            {
                files.Add(new UploadFile(str2));
            }
            return files;
        }

        public void RegisterProgressBar(WebControl m_controls)
        {
            m_controls.Attributes["onclick"] = "openProgress();";
            UploadStatus status = new UploadStatus();
            status.Status = UploadStatus.UploadState.Uploading;
            this.m_GUID = status.UploadGUID;
            HttpContext.Current.Application["Upload_Status_" + this.m_GUID] = status;
            string newValue = "progress.ashx?UploadID=" + this.m_GUID;
            this.m_script = this.m_script.Replace("${url}$", newValue);
            Page handler = (Page) WebbHelper.GetContext().Handler;
            handler.RegisterHiddenField("Webb_Upload_GUID", this.m_GUID);
            handler.RegisterStartupScript("ProgressScript", this.m_script);
            handler.Application.Add("Webb_Upload_GUID", this.m_GUID);
        }

        public void RegisterProgressBar(Button uploadButton, bool causesValidation)
        {
            if (causesValidation)
            {
                uploadButton.CausesValidation = false;
                uploadButton.Attributes["onclick"] = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();if(!Page_BlockSubmit){openProgress();}";
            }
            else
            {
                uploadButton.Attributes["onclick"] = "openProgress();";
            }
            UploadStatus status = new UploadStatus();
            status.Status = UploadStatus.UploadState.Uploading;
            this.m_GUID = status.UploadGUID;
            HttpContext.Current.Application["Upload_Status_" + this.m_GUID] = status;
            string newValue = "progress.ashx?UploadID=" + this.m_GUID;
            this.m_script = this.m_script.Replace("${url}$", newValue);
            Page handler = (Page) WebbHelper.GetContext().Handler;
            handler.RegisterHiddenField("Webb_Upload_GUID", this.m_GUID);
            handler.RegisterStartupScript("ProgressScript", this.m_script);
            handler.Application.Add("Webb_Upload_GUID", this.m_GUID);
        }

        public void SetTempPath(string m_path)
        {
            if (Directory.Exists(m_path))
            {
                ((Page) WebbHelper.GetContext().Handler).RegisterHiddenField("Webb_Upload_TempPath", m_path);
            }
        }
    }
}

